package cn.itsource.user.service.impl;

import cn.itsource.basic.constant.VerifyCodeConstant;
import cn.itsource.basic.exception.BusinessException;
import cn.itsource.basic.util.Md5Utils;
import cn.itsource.basic.util.StrUtils;
import cn.itsource.user.domain.Logininfo;
import cn.itsource.user.domain.User;
import cn.itsource.user.dto.UserDto;
import cn.itsource.user.mapper.LogininfoMapper;
import cn.itsource.user.mapper.UserMapper;
import cn.itsource.user.service.IUserService;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import com.sun.org.apache.regexp.internal.RE;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * 业务实现类：
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private LogininfoMapper logininfoMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    @Transactional
    public void add(User user) {
        //处理密码和盐值
        //盐值
        String salt = StrUtils.getComplexRandomString(32);
        user.setSalt(salt);
        //密码 - 加盐加密之后的
        String md5Pwd = Md5Utils.encrypByMd5(user.getPassword() + salt);
        user.setPassword(md5Pwd);

        Logininfo logininfo = user2Logininfo(user);
        logininfoMapper.save(logininfo);

        user.setLogininfoId(logininfo.getId());
        //调用父类的方法添加数据
        super.add(user);
    }

    @Override
    @Transactional
    public void del(Long id) {
        //根据id查询整个对象 - 为了获取对象的logininfo_id
        User user = userMapper.loadById(id);
        //删除user对象
        super.del(id);
        logininfoMapper.remove(user.getLogininfoId());
    }

    @Override
    @Transactional
    public void update(User user) {//参数user来自于前端，是没有logininfo_id的
        //处理密码和盐值
        //盐值
        String salt = StrUtils.getComplexRandomString(32);
        user.setSalt(salt);
        //密码 - 加盐加密之后的
        String md5Pwd = Md5Utils.encrypByMd5(user.getPassword() + salt);
        user.setPassword(md5Pwd);


        //根据id查询整个对象 - 为了获取对象的logininfo_id
        User oldUser = userMapper.loadById(user.getId());
        //更新user
        super.update(user);
        //查询以前的Logininfo对象
        Logininfo oldLogininfo = logininfoMapper.loadById(oldUser.getLogininfoId());

        //将新的数据拷贝到oldLogininfo对象中：type，disable都是以前的
        BeanUtils.copyProperties(user,oldLogininfo);
        //更新
        logininfoMapper.update(oldLogininfo);
    }

    @Override
    @Transactional
    public void phoneRegister(UserDto userDto) {
        String phone = userDto.getPhone();
        String code = userDto.getCode();
        String password = userDto.getPassword();
        String passwordRepeat = userDto.getPasswordRepeat();
        //1.校验
        //1.1.空值校验
        if(StringUtils.isEmpty(phone) || StringUtils.isEmpty(code)
                || StringUtils.isEmpty(password) || StringUtils.isEmpty(passwordRepeat)){
            throw new BusinessException("数据不能为空!!!");
        }
        //1.2.校验密码
        if(!password.equals(passwordRepeat)){
            throw new BusinessException("两次密码不一致!!!");
        }
        //1.3.校验手机验证码 - 是否过期，是否正确  【regisrer:1565566666 112355:154545454555】
        Object obj = redisTemplate.opsForValue().get(VerifyCodeConstant.REGISTER_CODE_PREFIX + phone);
        if(obj == null){
            throw new BusinessException("验证码过期!!!");
        }
        if(!obj.toString().split(":")[0].equals(code)){
            throw new BusinessException("验证码错误!!!");
        }

        //2.保存数据
        User user = userDto2User(userDto);
        Logininfo logininfo = user2Logininfo(user);
        //2.1.保存t_logininfo对象 - 获取自增id - 设置到user
        logininfoMapper.save(logininfo);
        //2.2.保存t_user对象
        user.setLogininfoId(logininfo.getId());
        userMapper.save(user);
    }

    //对象转对象就用这种方式
    private User userDto2User(UserDto userDto) {
        User user = new User();
        user.setUsername(userDto.getPhone());
        user.setPhone(userDto.getPhone());
        //盐值
        String salt = StrUtils.getComplexRandomString(32);
        user.setSalt(salt);
        //密码 - 加盐加密之后的
        String md5Pwd = Md5Utils.encrypByMd5(userDto.getPassword() + salt);
        user.setPassword(md5Pwd);
        user.setState(1);
        return user;
    }

    private Logininfo user2Logininfo(User user) {
        Logininfo logininfo = new Logininfo();
        //按照属性同名原则进行拷贝
        BeanUtils.copyProperties(user,logininfo);
        logininfo.setType(1);//1-表示用户
        return logininfo;
    }
}
